package com.unbox.admin.controller;

import com.unbox.admin.entity.Menu;
import com.unbox.admin.entity.Role;
import com.unbox.admin.entity.RoleMenu;
import com.unbox.admin.fegin.RoleFegin;
import com.unbox.admin.service.IRoleMenuService;
import com.unbox.admin.service.IRoleService;
import com.unbox.common.response.UnboxResponse;
import com.unbox.common.table.UnBoxPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * <角色业务Controller
 * </p>
 *
 * @author 王振宇
 * @since 2021-03-01
 */
@Api(tags = "角色管理api")
@RestController
public class RoleController implements RoleFegin {

    @Autowired
    IRoleService roleService;

    @Autowired
    IRoleMenuService roleMenuService;

    /**
     * 分页查询
     * @param role
     * @return
     */
    @ApiOperation(value = "分页查询列表")
    @Override
    public UnboxResponse<UnBoxPage> getPageList(Role role, Integer pageNo, Integer pageSize) {
        UnBoxPage<List<Role>> page = roleService.getPageList(role,pageNo, pageSize);
        List<Role> roles = page.getDatas();
        List<Integer> menuIds = roles.stream()
                .map(role1 -> role1.getId())
                .collect(Collectors.toList());

        // 获取role下面对应的
        if (menuIds.size() > 0) {
            List<RoleMenu> roleMenus = roleMenuService.getRoleIdsList(menuIds);
            roles.forEach(role1 -> {
               List<Menu> roleMenusVo = roleMenus.stream()
                        .filter(roleMenu -> role1.getId().equals(roleMenu.getRoleId()))
                       .map(roleMenu -> {
                           Menu menu = new Menu();
                           menu.setMenuId(roleMenu.getMenuId());
                           return menu;
                       })
                       .collect(Collectors.toList());
               role1.setRoleMenus(roleMenusVo);
            });
        }
        return new UnboxResponse<>(page);
    }

    /**
     * 获取角色列表
     * @param role
     * @return
     */
    @Override
    public UnboxResponse<List<Role>> getList(Role role) {
        return new UnboxResponse<List<Role>>(roleService.getList(role));
    }

    /**
     * 详情
     * @param role
     * @return
     */
    @ApiOperation(value = "查询明细")
    @Override
    public UnboxResponse<Role> info(Role role) {
        Role roleVo = roleService.get(role);
        return new UnboxResponse<>(roleVo);
    }

    /**
     * 通过id数据查询数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "通过ids进行in查询")
    @Override
    public UnboxResponse<List<Role>> idsRoles(List<Integer> ids) {
        List<Role> roles = roleService.getIdsList(ids);
        return new UnboxResponse<>(roles);
    }

    /**
     * 保存数据
     * @param role
     * @return
     */
    @ApiOperation(value = "保存数据")
    @Override
    public UnboxResponse<Boolean> save(Role role) {
        Boolean flg = roleService.save(role);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量添加
     * @param roles
     * @return
     */
    @ApiOperation(value = "批量保存数据")
    @Override
    public UnboxResponse<Boolean> batchSave(List<Role> roles) {
        Boolean flg = roleService.saveBatch(roles);
        return new UnboxResponse<>(flg);
    }

    /**
     * 修改数据
     * @param role
     * @return
     */
    @ApiOperation(value = "修改数据")
    @Override
    public UnboxResponse<Boolean> update(Role role) {
        Boolean flg = roleService.update(role);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量修改
     * @param roles
     * @return
     */
    @ApiOperation(value = "批量修改数据")
    @Override
    public UnboxResponse<Boolean> batchUpdate(List<Role> roles) {
        Boolean flg = roleService.updateBatchById(roles);
        return new UnboxResponse<>(flg);
    }

    /**
     * 删除数据
     * @param role
     * @return
     */
    @ApiOperation(value = "删除数据")
    @Override
    public UnboxResponse<Boolean> delete(Role role) {
        Boolean flg = roleService.remove(role);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @Override
    public UnboxResponse<Boolean> deleteBatch(List<Integer> ids) {
        Boolean flg = roleService.removeByIds(ids);
        return new UnboxResponse<>(flg);
    }
}

